package Servlets;

import PackDatos.GestionBD;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.data.general.DefaultPieDataset;

public class srv_estadisticaCaxCu extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        Connection cn = null;
        Map estadistica = new HashMap();
        Map rec = new HashMap();
        HttpSession session = request.getSession(true);
        try {
            //Devuelve un tipo imagen
            response.setContentType("image/jpeg");     
            int idcurso = Integer.parseInt(request.getParameter("curso"));
            // conectamos la BD 
            GestionBD og = new GestionBD();
            cn = og.conectar();
            String query = "select cur_nombre from curso where id_curso= " + idcurso;
            Statement stm = cn.createStatement();
            ResultSet rs = stm.executeQuery(query);
            while (rs.next()) {           
                rec.put("cur_nombre", rs.getString("cur_nombre"));
            }
            query = "select count(not_promfinal) from nota where not_promfinal>11 and id_curso=" + idcurso;
            stm = cn.createStatement();
            rs = stm.executeQuery(query);
            while (rs.next()) {
                estadistica.put("aprobado", rs.getInt("count(not_promfinal)"));
            }
            query = "select count(not_promfinal) from nota where not_promfinal<11 and id_curso=" + idcurso;
            stm = cn.createStatement();
            rs = stm.executeQuery(query);
            while (rs.next()) {            
                estadistica.put("desaprobado", rs.getInt("count(not_promfinal)"));
            }
            DefaultPieDataset ds = new DefaultPieDataset();
            String nombre = "";
            int apro=Integer.parseInt(estadistica.get("aprobado").toString());
            int desa=Integer.parseInt(estadistica.get("desaprobado").toString());
            ds.setValue("Nº Aprobados",apro );
            ds.setValue("Nº Desaprobados",desa );
            nombre = rec.get("cur_nombre").toString();
            JFreeChart graf = ChartFactory.createPieChart3D("Curso: " + nombre, ds, true, true, false);
            ChartFrame frame = new ChartFrame("Estadisticas del Curso: "+nombre , graf);
            frame.pack();
            frame.setVisible(true);
            session.setAttribute("frame", frame);
            cn.close();
        } catch (Exception e) {
            System.out.println("problemas al obtener cantidad de alumnos");
        }
    }
}
